/**
 * Created by Celebration-Grey on 2017/4/21.
 */

weibo.picture = {};

weibo.picture.count = 0;

weibo.picture.maxNum = 9;

weibo.picture.imgSrcs = [];

weibo.picture.imgUrls = [];

weibo.picture.$layer = null;

if(localStorage.getItem('pic_count') != null && localStorage.getItem('pic_count') > 0){
    weibo.picture.count = localStorage.getItem('pic_count');
    weibo.picture.imgSrcs = $.parseJSON(localStorage.getItem('pic_srcs'));
    weibo.picture.imgUrls = $.parseJSON(localStorage.getItem('pic_urls'));
}

weibo.picture.loadingLi = function () {
  var loading = '<li class="loading"><span class="txt S_txt1"><img class="picloading"src="http://img.t.sinajs.cn/t6/style/images/common/loading.gif"></span></li>';
    return $(loading);
};

weibo.picture.picLi = function (url) {
    var text = '<li class="pic"><div style="background:url('+url+') center center;width:80px;height:80px;"></div><span class="picbg"></span><a href="javascript:;"class="ico_delpic"></a></li>';
    return $(text);
};

weibo.picture.addLi = function () {
    var text = '<li class="add"style="position: relative;"><a href="javascript:;"title="">+</a><div style="position: absolute; left: 0px; top: 0px; display: block; overflow: hidden; opacity: 0; width: 80px; height: 80px; background-color: rgb(0, 0, 0);"><form id="pic_upload"name="pic_upload"target="upload_target"enctype="multipart/form-data"method="POST"style="overflow: hidden; opacity: 0; height: 80px; width: 80px;"><input type="file"name="pic"style="cursor:pointer;width:1000px;height:1000px;position:absolute;bottom:0;right:0;font-size:200px;"multiple="multiple"></form></div></li>';
    var $li = $(text);
    $li.find('input[name=pic]').change(function () {
        var $form = $(this).parents('form');
        weibo.picture.uploadPic($form);

        var $textarea = $('#bowen_content');
        if($textarea.val() == ''){
            $textarea.val('分享图片');
            $('#pub_btn').removeClass('W_btn_a_disable');
        }
    });
    return $li;
};

weibo.picture.uploadBox = function () {
    var text = '<div class="W_layer W_layer_pop"id="layer_pictures"style="left: 4px; top: 40px;"><div class="content"><div class="W_layer_close"><a href="javascript:void(0)"class="W_ficon ficon_close S_ficon glyphicon glyphicon-remove" style="top: 5px;"></a></div><div><div class="layer_pic_list clearfix"><div class="W_layer_con_tit"><h1 class="W_f14 W_fb">本地上传</h1><h2 class="clearfix"><span class="tit_txt W_fl">共<em class="num_totla">0</em>张，还能上传<em class="num_remain"></em>张</span></h2></div><ul class="drag_pic_list clearfix"></ul></div></div></div></div>';
    var $box = $(text);
    $box.find('.glyphicon-remove').click(function () {
        $(this).parents('.func_area').find('.picture-action').removeClass('btn_disable');
        $(this).parents('#layer_pictures').hide();
    });
    if(weibo.picture.count > 0){
        var urls = weibo.picture.imgUrls;
        for(var i = 0; i < urls.length; i++){
            $box.find('.drag_pic_list').append(weibo.picture.picLi(urls[i]));
        }
    }
    $box.find('em.num_totla').html(weibo.picture.count);
    $box.find('em.num_remain').html(weibo.picture.maxNum - weibo.picture.count);
    if (weibo.picture.count < weibo.picture.maxNum){
        $box.find('.drag_pic_list').append(weibo.picture.addLi());
    }
    return $box;
};

weibo.picture.numChange = function(){
    var $layer = $('#layer_pictures');
    $layer.find('em.num_totla').html(weibo.picture.count);
    $layer.find('em.num_remain').html(weibo.picture.maxNum - weibo.picture.count);
};

weibo.picture.picUpClick = function () {
    $('.picture-action').click(function () {
        $(this).parents('.func_area').find('.W_layer').remove();
        $(this).parents('.func_area').children('.kind').find('a').removeClass('btn_disable');
        $(this).addClass('btn_disable');
        $(this).parents('.func_area').append(weibo.picture.uploadBox());
    });
};

weibo.picture.uploadPic = function (form) {
    if(weibo.picture.count < weibo.picture.maxNum){
        var data = new FormData(form[0]);
        weibo.picture.beforeSend();
        $.ajax({
            url : host + 'weibo_bowenPicUpload',
            type : 'POST',
            dataType : 'json',
            data: data,
            processData: false,
            contentType: false,
            xhrFields: {
                withCredentials: true
            },
            success : weibo.picture.uploadSuccess,
            error : weibo.picture.error
        });
    }
};

weibo.picture.beforeSend = function () {
    var $layer = $('#layer_pictures');
    $layer.find('li.add').remove();
    $layer.find('ul.drag_pic_list').append(weibo.picture.loadingLi());
};

weibo.picture.uploadSuccess = function (data) {
    if(data.status == 200){
        weibo.picture.count++;
        weibo.picture.imgUrls.push(data.data['url']);
        weibo.picture.imgSrcs.push(data.data['src']);
        weibo.picture.numChange();
        weibo.picture.localUpdate();
        var $layer = $('#layer_pictures');
        $layer.find('li.loading').remove();
        $layer.find('ul.drag_pic_list').append(weibo.picture.picLi(data.data['url']));
        if(weibo.picture.count < weibo.picture.maxNum){
            $layer.find('ul.drag_pic_list').append(weibo.picture.addLi());
        }
    }
    else{
        $.Huimodalalert(data.message,2000);
    }
};

weibo.picture.error = function (XMLHttpRequest, textStatus, errorThrown) {
    console.log(XMLHttpRequest);
    console.log(textStatus);
    console.log(errorThrown);
};

weibo.picture.localUpdate = function(){
    localStorage.setItem('pic_count',weibo.picture.count);
    localStorage.setItem('pic_urls',JSON.stringify(weibo.picture.imgUrls));
    localStorage.setItem('pic_srcs',JSON.stringify(weibo.picture.imgSrcs));
};

weibo.picture.clearLocal = function () {
    localStorage.setItem('pic_count',0);
    localStorage.setItem('pic_urls','');
    localStorage.setItem('pic_srcs','');
    weibo.picture.count = 0;
    weibo.picture.imgSrcs = [];
    weibo.picture.imgUrls = [];
    $('.picture-action.btn_disable').removeClass('.btn_disable');
};